package dome3;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Text {
    public static void calaWordNum(String[] words) {
        Map<String,Integer> map = new HashMap<>();
        for (String word : words) {
            if(map.get(word) != null) {
                int oldNum = map.get(word);
                map.put(word,oldNum+1);
            }else {
                map.put(word,1);
            }
        }
        for (Map.Entry<String,Integer> entry : map.entrySet()) {
            System.out.println("单词 " + entry.getKey() + " 出现了 " + entry.getValue() + " 次!");
        }
    }
    public static void main(String[] args) {
        //统计每单词出现的次数
        String[] words = {"this","is","good","man","this","good","good"};
        calaWordNum(words);
    }
    public static void main2(String[] args) {
        //10W个数据去重
        int[] array = {1,2,3,4,5,6,3,2,1};
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < array.length; i++) {
            set.add(array[i]);
        }
        System.out.println(set);
    }
    public static void main1(String[] args) {
        //找到10W个数据中第一个重复的数据
        int[] array = {1,2,3,4,5,6,3,2,1};
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < array.length; i++) {
            if(!set.contains(array[i])) {
                set.add(array[i]);
            }else {
                System.out.println(array[i]);
                return;
            }
        }
    }
}
